package com.liugh.controller.zhongche;

import com.baomidou.mybatisplus.plugins.Page;
import com.liugh.annotation.CurrentUser;
import com.liugh.base.BusinessException;
import com.liugh.base.PublicResultConstant;
import com.liugh.config.ResponseModel;
import com.liugh.controller.BaseRestController;
import com.liugh.entity.User;
import com.liugh.entity.standard.StandardMessage;
import com.liugh.service.zhongche.IStandardMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Api(description="留言模块")
@RestController
@RequestMapping("/message")
public class StandardMessageController extends BaseRestController {

    @Autowired
    IStandardMessageService messageService;

    /**
     * 增加留言
     */
    @PostMapping("/add")
    @ApiOperation("添加留言")
    public ResponseModel<StandardMessage> addMessage(@RequestBody StandardMessage message,
                                                     @CurrentUser User user) throws BusinessException {
        message.setUserNo(user.getUserNo());
        message.setCreateTime(new Date());
        if (messageService.addMessage(message) == null) {
            throw new BusinessException("添加失败");
        }
        return fillSuccessData(message);
    }

    @ApiOperation("删除留言，管理员操作")
    @ApiImplicitParam(name = "id",value = "留言ID")
    @DeleteMapping("/delete_id")
    public ResponseModel<String> deleteMessage(@RequestParam("id")int id) {
        boolean isSuccess = messageService.deleteMessageById(id);
        if (!isSuccess) {
            return fillFailData(PublicResultConstant.ERROR);
        }
        return fillSuccessData(PublicResultConstant.SUCCEED);
    }


    @ApiOperation(value = "分页获取所有的留言")
    @GetMapping("/all")
    public ResponseModel<Page<StandardMessage>> getAllMessage(@RequestParam(name = "pageNum",defaultValue = "1",required = false)int pageNum,
                                                              @RequestParam(name = "pageSize",defaultValue = "10",required = false)int pageSize) {
        Page<StandardMessage> page = new Page<>(pageNum,pageSize);
        return fillSuccessDataPage(messageService.getAllMsgPage(page));
    }

    @ApiOperation(value = "获取我的留言",notes = "管理员也可根据用户编号查看")
    @GetMapping("/my")
    @ApiImplicitParam(name = "is_asc",value = "是否根据时间逆序，默认为false",dataType = "boolean",paramType = "query")
    public ResponseModel<Page<StandardMessage>> getMyMsgPage(@RequestParam(name = "pageNum",defaultValue = "1",required = false)int pageNum,
                                                       @RequestParam(name = "pageSize",defaultValue = "10",required = false)int pageSize,
                                                       @RequestParam(name = "is_asc",defaultValue = "false",required = false)boolean isAsc,
                                                       @CurrentUser User user) {
        Page<StandardMessage> page = new Page<>(pageNum,pageSize);
        return fillSuccessDataPage(messageService.getMyMsgPage(page,user.getUserNo(),isAsc));
    }


    /**
     * 删除我的留言 .
     * @param id .
     * @param user .
     * @return .
     */
    @ApiOperation("删除我的留言")
    @DeleteMapping("/delete_my")
    public ResponseModel<String> deleteMyMessage(@RequestParam("id")int id,@CurrentUser User user) {
        boolean isSuccess = messageService.delMessage(id, user);
        if (!isSuccess) {
            return fillSuccessData(PublicResultConstant.ERROR);
        }
        return fillSuccessData(PublicResultConstant.SUCCEED);
    }


    /**
     * 根据id获得留言 .
     * @param id .
     * @return .
     */
    @ApiOperation("根据id获得留言")
    @GetMapping("/get")
    public ResponseModel<StandardMessage> getMessageById(@RequestParam("id") int id) {
        return fillSuccessData(messageService.getMessageById(id));
    }



    /**
     * 修改留言 .
     * @param standardMessage .
     * @return .
     */
    @ApiOperation("根据id修改留言")
    @PutMapping("/update")
    public ResponseModel<StandardMessage> update(@RequestBody StandardMessage standardMessage) throws BusinessException {
        StandardMessage message = messageService.updateMessageById(standardMessage);
        if (message == null) {
            throw new BusinessException("更新失败");
        }
        return fillSuccessData(standardMessage);
    }
}
